package org.example.controller;

import org.example.DTO.OrderDTO;
import org.example.Service.OrderDTOService;
import org.example.Service.OrderDetailsService;
import org.example.Service.OrderService;
import org.example.Service.ProductService;
import org.example.config.SessionManager;
import org.example.pojo.Order;
import org.example.pojo.OrderDetails;
import org.example.pojo.Product;
import org.example.pojo.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailsService orderDetailsService;
    @Autowired
    private OrderDTOService orderDTOService;

    //查询所有订单
    @RequestMapping("/allOrder")
    public String selectOrders(Model model) {
        List<Order> list = orderService.selectOrders();
        model.addAttribute("list", list);
        return "userOrderIng";
    }

    //查询该orderID的订单
    @RequestMapping("/selectOrderByID")
    public String selectOrderByID(Integer order_id, Model model) {
        Order order = orderService.selectOrderByID(order_id);
        System.out.println(order);
        List<Order> list = new ArrayList<>();
        list.add(order);
        if (order == null) {
            list = orderService.selectOrders();
            model.addAttribute("error", "未找到!");
        }
        model.addAttribute("list", list);
        return "userOrderIng";
    }

    //查询所有已发货的订单
    @RequestMapping("/allOrdersFaHuo")
    public String selectOrdersFaHuo(Model model) {
        List<Order> list = orderService.selectOrdersFaHuo();
        model.addAttribute("list", list);
        return "shippingInfoFaHuo";
    }

    //查询所有已下单的订单
    @RequestMapping("/allOrdersIng")
    public String selectOrdersIng(Model model) {
        List<Order> list = orderService.selectOrdersIng();
        model.addAttribute("list", list);
        return "shippingInfoIng";
    }

    //查询所有已取消的订单
    @RequestMapping("/allOrdersCancel")
    public String selectOrdersCancel(Model model) {
        List<Order> list = orderService.selectOrdersCancel();
        model.addAttribute("list", list);
        return "shippingCancel";
    }
    //查询该orderID的进行中的订单


    //查询该orderID的已发货的订单
    @RequestMapping("/selectOrderByIDFaHuo")
    public String selectOrderByIDFaHuo(Integer order_id, Model model) {
        Order order = orderService.selectOrderByOrderIDFaHuo(order_id);
        List<Order> list = new ArrayList<>();
        list.add(order);
        if (order == null) {
            list = orderService.selectOrdersFaHuo();
            model.addAttribute("error", "未找到!");
        }
        if (order_id == null) {
            list = orderService.selectOrdersFaHuo();
            model.addAttribute("error", "请输入订单编号!");
        }
        model.addAttribute("list", list);
        return "shippingInfoFaHuo";
    }

    //查询该orderID的已下单的订单
    @RequestMapping("/selectOrderByIDIng")
    public String selectOrderByOrderIDIng(Integer order_id, Model model) {
        Order order = orderService.selectOrderByOrderIDIng(order_id);
        List<Order> list = new ArrayList<>();
        list.add(order);
        if (order == null) {
            list = orderService.selectOrdersCancel();
            model.addAttribute("error", "未找到!");
        }
        if (order_id == null) {
            list = orderService.selectOrdersCancel();
            model.addAttribute("error", "请输入订单编号!");
        }
        model.addAttribute("list", list);
        return "shippingInfoIng";
    }

    //查询该orderID的已取消的订单
    @RequestMapping("/selectOrderByIDCancel")
    public String selectOrderByOrderIDCancel(Integer order_id, Model model) {
        Order order = orderService.selectOrderByOrderIDCancel(order_id);
        List<Order> list = new ArrayList<>();
        list.add(order);
        if (order == null) {
            list = orderService.selectOrdersCancel();
            model.addAttribute("error", "未找到!");
        }
        if (order_id == null) {
            list = orderService.selectOrdersCancel();
            model.addAttribute("error", "请输入订单编号!");
        }
        model.addAttribute("list", list);
        return "shippingCancel";
    }


    //查询该用户的所有订单
    @RequestMapping("/allUserOrder")
    public String selectOrderUserID(Integer user_id, Model model) {
        List<Order> list = orderService.selectOrderByUserID(user_id);
        System.out.println(list);
        model.addAttribute("list", list);
        return "userOrderIng";
    }

    //查询该用户id待收货的所有订单
    @RequestMapping("/allUserOrderFaHuo")
    public String selectUserOrderFaHuo(Model model) {
        Integer user_id = SessionManager.getUserId();
        List<Order> list = orderService.selectUserOrdersFaHuo(user_id);
        model.addAttribute("list", list);
        return "userOrderFaHuo";
    }

    //查询该用户id待收货的某一订单
    @RequestMapping("/selectUserOrderByIDFaHuo")
    public String selectUserOrderByIDFaHuo(Integer order_id, Model model) {
        Integer user_id = SessionManager.getUserId();
        Order order = orderService.selectUserOrderByOrderIDFaHuo(user_id, order_id);
        System.out.println(order);
        List<Order> list = new ArrayList<>();
        list.add(order);
        if (order == null) {
            list = orderService.selectUserOrdersFaHuo(user_id);
            model.addAttribute("error", "未找到!");
        }
        if (order_id == null) {
            list = orderService.selectUserOrdersFaHuo(user_id);
            model.addAttribute("error", "请输入订单编号!");
        }
        model.addAttribute("list", list);
        return "userOrderFaHuo";
    }

    //查询该用户id待发货的所有订单
    @RequestMapping("/allUserOrderIng")
    public String selectUserOrderIng(Model model) {
        Integer user_id = SessionManager.getUserId();
        List<Order> list = orderService.selectUserOrdersIng(user_id);
        model.addAttribute("list", list);
        return "userOrderIng";
    }

    //查询该用户id待发货的某一订单
    @RequestMapping("/selectUserOrderByIDIng")
    public String selectOrderByIDIng(Integer order_id, Model model) {
        Integer user_id = SessionManager.getUserId();
        Order order = orderService.selectUserOrderByOrderIDIng(user_id, order_id);
        System.out.println(order);
        List<Order> list = new ArrayList<>();
        list.add(order);
        if (order == null) {
            list = orderService.selectUserOrdersIng(user_id);
            model.addAttribute("error", "未找到!");
        }
        if (order_id == null) {
            list = orderService.selectUserOrdersIng(user_id);
            model.addAttribute("error", "请输入订单编号!");
        }
        model.addAttribute("list", list);
        return "userOrderIng";
    }

    //查询该用户id已取消的所有订单
    @RequestMapping("/allUserOrderCancel")
    public String selectUserOrderCancel(Model model) {
        Integer user_id = SessionManager.getUserId();
        List<Order> list = orderService.selectUserOrdersCancel(user_id);
        model.addAttribute("list", list);
        return "userOrderCancel";
    }

    //查询该用户id已取消的某一订单
    @RequestMapping("/selectUserOrderByIDCancel")
    public String selectUserOrderByIDCancel(Integer order_id, Model model) {
        Integer user_id = SessionManager.getUserId();
        Order order = orderService.selectUserOrderByOrderIDCancel(user_id, order_id);
        List<Order> list = new ArrayList<>();
        list.add(order);
        if (order == null) {
            list = orderService.selectUserOrdersCancel(user_id);
            model.addAttribute("error", "未找到!");
        }
        if (order_id == null) {
            list = orderService.selectUserOrdersCancel(user_id);
            model.addAttribute("error", "请输入订单编号!");
        }
        model.addAttribute("list", list);
        return "userOrderCancel";
    }

    //跳转到添加订单页面
    @RequestMapping("/toInsertOrder")
    public String toInsertOrder(Model model, Integer id) {
        Product product = productService.selectProductByID(id);
        model.addAttribute("product", product);
        return "insertOrder";
    }

    //添加订单信息
    @RequestMapping("/insertOrder")
    public String insertOrder(OrderDTO orderDTO) {
        orderDTOService.insertOrderDTO(orderDTO);
        return "userOrderIng";
    }

    //取消订单
    @RequestMapping("/CancelOrder/{id}")
    public String CancelOrder(@PathVariable("id") Integer id, Model model) {
        Order order = orderService.selectOrderByID(id);
        order.setStatus(Status.已取消);
        orderService.updateOrder(order);
        model.addAttribute("order", order);
        return "userOrderIng";
    }


    //跳转到修改订单详情页面
    @RequestMapping("/toUpdateOrderDetails/{id}")
    public String toUpdateOrder(@PathVariable("id") Integer order_id, Model model) {
        OrderDetails orderDetails = orderDetailsService.selectOrderDetailsByOrderID(order_id);
        model.addAttribute("orderDetails", orderDetails);
        return "updateOrderDetails";
    }

    //修改订单详情信息
    @RequestMapping("/updateOrderDetails")
    public String updateOrder(OrderDetails orderDetails) {
        orderDetailsService.updateOrderDetails(orderDetails);
        return "userOrderIng";
    }

    //删除订单信息
    @RequestMapping("/deleteOrder/{id}")
    public String deleteOrder(@PathVariable("id") Integer id) {
        orderDTOService.deleteOrderDTO(id);
        return "shippingCancel";
    }
}
